Typical HAL OPAMP use cases for migration ¶
This section provides information about typical HAL OPAMP use cases for migration.
Use case: OPAMP in PGA mode ¶
This use case employs the OPAMP (operational amplifier) in PGA (Programmable Gain Amplifier) mode to boost a differential analog signal. The amplified output is then digitized by the ADC (Analog-to-Digital Converter) for further processing.
Description of usage ¶
|
Description |
HAL1 |
HAL2 |
|---|---|---|
|
OPAMP initialization |
Set OPAMP parameters and initialize
|
Initialize OPAMP peripheral and set gain
|
|
ADC initialization |
Configure ADC parameters and channel:
|
Initialize ADC peripheral and configure advanced features
|
|
Start OPAMP and ADC |
Enable OPAMP and start ADC conversion
|
Enable OPAMP and start ADC conversion with status check
|
|
Continuous ADC reading loop |
Poll for ADC conversion completion and retrieve value
|
Poll ADC for conversion and read value continuously
|
|
Stop OPAMP and ADC |
Stop OPAMP and ADC when no longer needed
|
Stop OPAMP and ADC when no longer needed
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
OPAMP_HandleTypeDef hopamp1;
ADC_HandleTypeDef hadc1;
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
hopamp1.Instance = OPAMP1;
// Configure opamp in differential amplifier mode
hopamp1.Init.Mode = OPAMP_PGA_MODE;
hopamp1.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0;
hopamp1.Init.InvertingInput = OPAMP_INVERTINGINPUT_IO1;
hopamp1.Init.PgaGain = OPAMP_PGA_GAIN_2;
hopamp1.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;
if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
{
// Initialization Error
Error_Handler();
}
ADC_ChannelConfTypeDef sConfig = {0};
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
// Configure ADC channel connected to OPAMP output
sConfig.Channel = ADC_CHANNEL_OPAMP1; // Channel connected to OPAMP1 output
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
// Start the opamp
if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
{
// Error handling
Error_Handler();
}
// Start ADC
if (HAL_ADC_Start(&hadc1) != HAL_OK)
{
// Error handling
Error_Handler();
}
while (1)
{
// Poll ADC conversion
if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) == HAL_OK)
{
uint32_t adcValue = HAL_ADC_GetValue(&hadc1);
// adcValue now contains the amplified difference signal
// Process adcValue as needed
}
}
}
void Error_Handler(void)
{
// User can add error handling here
while (1)
{
}
}
|
hal_opamp_handle_t hopamp1;
hal_adc_handle_t hadc1;
hal_opamp_config_t hconfig;
hal_adc_config_t adc_config;
hal_adc_post_processing_config_t adc_config_post_processing;
hal_adc_low_power_config_t adc_config_low_power;
hal_adc_reg_config_t adc_reg_config;
hal_adc_ovs_config_t adc_ovs_config;
hal_opamp_config_input_connection_t hconfig_input;
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
// Configure opamp in differential amplifier mode
hconfig.configuration_mode = HAL_OPAMP_MODE_PGA;
hconfig_input.inverting_input = HAL_OPAMP_INVERTING_INPUT_GPIO_1;
hconfig_input.non_inverting_input = HAL_OPAMP_NON_INVERTING_INPUT_GPIO ;
if (HAL_OPAMP_Init(&hopamp1,OPAMP1) != HAL_OK)
{
// Initialization Error
Error_Handler();
}
if (HAL_OPAMP_SetConfig(&hopamp1, &hconfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_OPAMP_SetConfigInputConnection(&hopamp1, &hconfig_input) != HAL_OK)
{
Error_Handler();
}
HAL_OPAMP_SetGain(&hopamp1,HAL_OPAMP_PGA_GAIN_2);
if (HAL_ADC_Init(&hadc1, ADC1) != HAL_OK)
{
Error_Handler();
}
hadc1.Init.ClockPrescaler = HAL_ADC_CLOCK_SYNC_DIV4;
hadc1.Init.Resolution = HAL_ADC_RESOLUTION_12_BIT;
adc_config_post_processing.left_bit_shift = HAL_ADC_LEFT_BIT_SHIFT_NONE;
if (HAL_ADC_SetConfigPostProcessing(hadc, &adc_config_post_processing) != HAL_OK)
{
Error_Handler();
}
adc_config_low_power.lp_auto_wait = HAL_ADC_LP_AUTO_WAIT_DISABLE;
if (HAL_ADC_SetConfigLowPower(hadc, &adc_config_low_power) != HAL_OK)
{
Error_Handler();
}
adc_reg_config.sequencer_mode = HAL_ADC_REG_SEQ_FIXED;
adc_reg_config.continuous = HAL_ADC_REG_CONV_CONTINUOUS;
adc_reg_config.sequencer_length = 1;
adc_reg_config.sequencer_discont = HAL_ADC_REG_SEQ_DISCONT_DISABLE;
adc_reg_config.trigger_src = HAL_ADC_REG_TRIG_SOFTWARE_START;
adc_reg_config.trigger_edge = HAL_ADC_REG_TRIG_EDGE_NONE;
adc_reg_config.data_transfer = HAL_ADC_REG_DATA_TRANSFER_NONE;
adc_reg_config.overrun = HAL_ADC_REG_OVR_DATA_PRESERVED;
if (HAL_ADC_REG_SetConfig(hadc, &adc_reg_config) != HAL_OK)
{
Error_Handler();
}
// Configure ADC channel connected to OPAMP output
adc_channel_config.sequencer_rank = 1;
adc_channel_config.sampling_time = HAL_ADC_SAMPLING_TIME_247CYCLES_5;
if (HAL_ADC_SetConfigChannel(hadc, HAL_ADC_CHANNEL_8, &adc_channel_config) != HAL_OK)
{
Error_Handler();
}
// Start the opamp
if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
{
// Error handling
Error_Handler();
}
// Start ADC
if (HAL_ADC_Start(&hadc1) != HAL_OK)
{
// Error handling
Error_Handler();
}
while (1)
{
// Poll ADC conversion
if (HAL_ADC_REG_PollForConv(&hadc1, HAL_MAX_DELAY) == HAL_OK)
{
uint32_t adcValue = HAL_ADC_REG_GetValue(&hadc1);
// adcValue now contains the amplified difference signal
// Process adcValue as needed
}
}
}
void Error_Handler(void)
{
// User can add error handling here
while (1)
{
}
}
|
Key changes ¶
HAL2 splits configuration from initialization using HAL_OPAMP_SetConfig() (and configures gain with HAL_OPAMP_SetGain()).
Use case: OPAMP in follower mode ¶
The use case sets up the OPAMP as a voltage follower to buffer an analog input, ensuring accurate and stable signal transfer, and includes a self-calibration step for optimal operation.
Description of usage ¶
|
Description |
HAL1 |
HAL2 |
|---|---|---|
|
System Initialization |
Initialize HAL library and system peripherals
|
Initialize HAL library and system peripherals
|
|
GPIO Configuration for OPAMP Input |
Enable GPIOx clock
|
Enable GPIOx clock
|
|
OPAMP Initialization |
Set OPAMP parameters individually then:
|
Set OPAMP parameters via config struct then:
|
|
OPAMP Self-Calibration |
Call
|
Call
|
|
Start OPAMP |
Call
|
Call
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
OPAMP_HandleTypeDef hopamp1;
void SystemClock_Config(void);
void Error_Handler(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
// Configure GPIO pin for OPAMP input (example: PA0)
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// 1. Initialize OPAMP handle
hopamp1.Instance = OPAMP1;
// 2. Configure OPAMP in follower mode
hopamp1.Init.Mode = OPAMP_FOLLOWER_MODE;
hopamp1.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; // PA0 for example
hopamp1.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
hopamp1.Init.UserTrimming = OPAMP_TRIMMING_USER; // Use user trimming after calibration
if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
{
Error_Handler();
}
// 3. Perform self-calibration to get trimming values
if (HAL_OPAMP_SelfCalibrate(&hopamp1) != HAL_OK)
{
Error_Handler();
}
// 4. Start OPAMP
if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
{
Error_Handler();
}
/* Add your processing code here */
}
void SystemClock_Config(void)
{
// Configure system clock as needed
}
void Error_Handler(void)
{
while(1);
}
|
hal_opamp_handle_t hopamp1;
void SystemClock_Config(void);
void Error_Handler(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
// Configure GPIO pin for OPAMP input
HAL_RCC_GPIOA_CLK_ENABLE();
hal_gpio_config_t GPIO_Config;
GPIO_Config.Mode = HAL_GPIO_MODE_ANALOG;
GPIO_Config.Pull = HAL_GPIO_PULL_NO;
HAL_GPIO_Init(GPIOA, HAL_GPIO_PIN_0, &GPIO_Config);
// 1. Initialize OPAMP handle
hopamp1.Instance = OPAMP1;
// 2. Configure OPAMP in follower mode
hconfig.configuration_mode = HAL_OPAMP_MODE_FOLLOWER;
if (HAL_OPAMP_SetConfig(&hopamp1, &hconfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
{
Error_Handler();
}
// 3. Perform self-calibration to get trimming values
if (HAL_OPAMP_Calibrate(&hopamp1 , HAL_OPAMP_POWER_MODE_NORMAL) != HAL_OK)
{
Error_Handler();
}
// 4. Start OPAMP
if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
{
Error_Handler();
}
/* Add your processing code here */
}
void SystemClock_Config(void)
{
// Configure system clock as needed
}
void Error_Handler(void)
{
while(1);
}
|
Key changes ¶
In HAL2, OPAMP configuration is provided using a configuration structure with
HAL_OPAMP_SetConfig().The calibration API changes from
HAL_OPAMP_SelfCalibrate()(HAL1) toHAL_OPAMP_Calibrate()(HAL2).
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
follower |
How to configure, with the HAL API, an embedded amplifier (OPAMP) in follower mode. It also shows how to use an internal ADC peripheral to read the OPAMP output voltage. |
Use case: OPAMP in standalone mode ¶
The use case configures the OPAMP in standalone mode, providing maximum flexibility for custom analog signal processing with external components, and ensures reliable operation through self-calibration.
Description of usage ¶
|
Description |
HAL1 |
HAL2 |
|---|---|---|
|
System Initialization |
Initialize HAL library and system peripherals
|
Initialize HAL library and system peripherals
|
|
GPIO Configuration for OPAMP Input |
Enable GPIOx clock
|
Enable GPIOx clock
|
|
OPAMP Initialization |
Set OPAMP parameters individually then
Call
|
Set OPAMP parameters via config struct then
Call
|
|
OPAMP Self-Calibration |
Call
|
Call
|
|
Start OPAMP |
Call
|
Call
|
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
OPAMP_HandleTypeDef hopamp1;
void SystemClock_Config(void);
void Error_Handler(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
// Configure GPIO pin for OPAMP input (example: PA0)
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// 1. Initialize OPAMP handle
hopamp1.Instance = OPAMP1;
// 2. Configure OPAMP in standalone mode
hopamp1.Init.Mode = OPAMP_STANDALONE_MODE;
hopamp1.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0;
hopamp1.Init.InvertingInput = OPAMP_INVERTINGINPUT_IO0;
hopamp1.Init.PowerSupplyRange = OPAMP_POWERSUPPLY_HIGH;
hopamp1.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
hopamp1.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;
if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
{
Error_Handler();
}
// 3. Perform self-calibration to get trimming values
if (HAL_OPAMP_SelfCalibrate(&hopamp1) != HAL_OK)
{
Error_Handler();
}
// 4. Start OPAMP
if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
{
Error_Handler();
}
/* Add your processing code here */
}
void SystemClock_Config(void)
{
// Configure system clock as needed
}
void Error_Handler(void)
{
while(1);
}
|
hal_opamp_handle_t hopamp1;
hal_opamp_config_t hconfig;
hal_opamp_config_input_connection_t hconfig_input;
void SystemClock_Config(void);
void Error_Handler(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
// Configure GPIO pin for OPAMP input
HAL_RCC_GPIOA_CLK_ENABLE();
hal_gpio_config_t GPIO_Config;
GPIO_Config.Mode = HAL_GPIO_MODE_ANALOG;
GPIO_Config.Pull = HAL_GPIO_PULL_NO;
HAL_GPIO_Init(GPIOA, HAL_GPIO_PIN_7, &GPIO_Config);
// 1. Initialize OPAMP handle
if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
{
Error_Handler();
}
// 2. Configure OPAMP in standalone mode
hconfig.configuration_mode = HAL_OPAMP_MODE_STANDALONE;
hconfig.power_supply_range = HAL_OPAMP_POWER_SUPPLY_HIGH;
hconfig.power_mode = HAL_OPAMP_POWER_MODE_NORMAL ;
hconfig_input.inverting_input = HAL_OPAMP_INVERTING_INPUT_GPIO_0;
hconfig_input.non_inverting_input = HAL_OPAMP_NON_INVERTING_INPUT_GPIO ;
if (HAL_OPAMP_SetConfig(&hopamp1, &hconfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_OPAMP_SetConfigInputConnection(&hopamp1, &hconfig_input) != HAL_OK)
{
Error_Handler();
}
// 3. Perform self-calibration to get trimming values
if (HAL_OPAMP_Calibrate(&hopamp1 , HAL_OPAMP_POWER_MODE_NORMAL) != HAL_OK)
{
Error_Handler();
}
// 4. Start OPAMP
if (HAL_OPAMP_Start(&hopamp1) != HAL_OK)
{
Error_Handler();
}
/* Add your processing code here */
}
void SystemClock_Config(void)
{
// Configure system clock as needed
}
void Error_Handler(void)
{
while(1);
}
|
Key changes ¶
In HAL2, OPAMP configuration is provided using a configuration structure with
HAL_OPAMP_SetConfig()and input connections are configured withHAL_OPAMP_SetConfigInputConnection().The calibration API changes from
HAL_OPAMP_SelfCalibrate()(HAL1) toHAL_OPAMP_Calibrate()(HAL2).
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
standalone |
How to use the OPAMP in standalone mode. |





